package IC.Parser;

import java_cup.runtime.Symbol; 
import java.lang.reflect.Field;

/**
 * This Class represent the Token Object.
 */
public class Token extends Symbol {
	 
	//Data members:
	private int id;
	private int line;
	private Object value=null;
	private String Name;
	
	//C'tor
    public Token(int id, int line) {
    	this(id,null,line);
    }
    
    //C'tor
    public Token(int id, Object value,int line){
    	super(id, value);
    	this.id=id;
        this.line=line;
        this.value=value;  
        this.Name = reflectTokenName();
    }
    
    //Getters:
    public int getId() 
    {
    	return this.id;
    }
    
    public String getName() 
    {
    	return this.Name;
    }
    
	public Object getValue() 
	{
		return this.value;
	}  
	
	public int getLine() 
	{
		return this.line;
	}
	
	//toString
	public String toString() 
	{   
		if (this.value!=null)
			return this.line + ": " + this.Name + "(" + this.value + ")";
		return this.line + ": " + this.Name;
	}  
	
	//get token Name from Sym Class:
	private String reflectTokenName() 
	{                  
		try {        
			String Name=null;
			for (Field f : sym.class.getFields())   {
				if (f.getInt(sym.class)==this.id) {
					Name=f.getName(); 
					break;
				}
			}
			return Name;
			} 
		catch (Exception e) {                          
			return null;                  
			}          
		}                        

}

